package com.tianji.learning.mq;

import com.tianji.common.constants.MqConstants;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.mq.message.SignInMessage;
import com.tianji.learning.service.IPointsRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class LearningPointListener {


    private final IPointsRecordService pointsRecordService;

    /**
     * 签到积分
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "sign.points.queue", durable = "true"),
    exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
    key = MqConstants.Key.SIGN_IN))
    public void listenSignInListener(SignInMessage msg){
        log.debug("签到增加积分  消费到消息  {}", msg);
        pointsRecordService.addPointRecord(msg, PointsRecordType.SIGN);
    }

    /**
     * 问答积分
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "qa.points.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.WRITE_REPLY))
    public void listenReplyListener(SignInMessage msg){
        log.debug("问答增加积分  消费到消息  {}", msg);
        pointsRecordService.addPointRecord(msg, PointsRecordType.QA);
    }

    /**
     * 考试学习积分
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "learning.points.queue", durable = "true"),
            exchange = @Exchange(value = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.WRITE_REPLY))
    public void listenLearningListener(SignInMessage msg){
        log.debug("学习增加积分  消费到消息  {}", msg);
        pointsRecordService.addPointRecord(msg, PointsRecordType.LEARNING);
    }




}
